home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / Mesa / src-glut / glutShapes.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  14KB  |  594 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994, 1997. */
  3.  
  4. /**
  5. (c) Copyright 1993, Silicon Graphics, Inc.
  6.  
  7. ALL RIGHTS RESERVED
  8.  
  9. Permission to use, copy, modify, and distribute this software
  10. for any purpose and without fee is hereby granted, provided
  11. that the above copyright notice appear in all copies and that
  12. both the copyright notice and this permission notice appear in
  13. supporting documentation, and that the name of Silicon
  14. Graphics, Inc. not be used in advertising or publicity
  15. pertaining to distribution of the software without specific,
  16. written prior permission.
  17.  
  18. THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
  19. "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
  20. OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  21. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  22. EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
  23. ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
  24. CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  25. INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
  26. SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
  27. NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
  28. OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
  30. PERFORMANCE OF THIS SOFTWARE.
  31.  
  32. US Government Users Restricted Rights
  33.  
  34. Use, duplication, or disclosure by the Government is subject to
  35. restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  36. (c)(1)(ii) of the Rights in Technical Data and Computer
  37. Software clause at DFARS 252.227-7013 and/or in similar or
  38. successor clauses in the FAR or the DOD or NASA FAR
  39. Supplement.  Unpublished-- rights reserved under the copyright
  40. laws of the United States.  Contractor/manufacturer is Silicon
  41. Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
  42. 94039-7311.
  43.  
  44. OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  45. */
  46.  
  47.  
  48. /*
  49.  * glutShapes.c
  50.  *
  51.  * Modified  27 Jun 1998
  52.  * by Jarno van der Linden
  53.  * jarno@kcbbs.gen.nz
  54.  *
  55.  * Based on glut_shapes.c
  56.  * Some changes to work with Amiga GLUT
  57.  *
  58.  */
  59.  
  60.  
  61. #include <math.h>
  62.  
  63. #include "glutstuff.h"
  64.  
  65. /* Some <math.h> files do not define M_PI... */
  66. #ifndef M_PI
  67. #define M_PI 3.14159265358979323846
  68. #endif
  69.  
  70. static GLUquadricObj *quadObj;
  71.  
  72. #define QUAD_OBJ_INIT() { if(!quadObj) initQuadObj(); }
  73.  
  74. static void
  75. initQuadObj(void)
  76. {
  77.   quadObj = gluNewQuadric();
  78. //  if (!quadObj)
  79. //    __glutFatalError("out of memory.");
  80. }
  81.  
  82. /* CENTRY */
  83. void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
  84. {
  85.   QUAD_OBJ_INIT();
  86.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  87.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  88.   /* If we ever changed/used the texture or orientation state
  89.      of quadObj, we'd need to change it to the defaults here
  90.      with gluQuadricTexture and/or gluQuadricOrientation. */
  91.   gluSphere(quadObj, radius, slices, stacks);
  92. }
  93.  
  94. void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
  95. {
  96.   QUAD_OBJ_INIT();
  97.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  98.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  99.   /* If we ever changed/used the texture or orientation state
  100.      of quadObj, we'd need to change it to the defaults here
  101.      with gluQuadricTexture and/or gluQuadricOrientation. */
  102.   gluSphere(quadObj, radius, slices, stacks);
  103. }
  104.  
  105. void glutWireCone(GLdouble base, GLdouble height, GLint slices, GLint stacks)
  106. {
  107.   QUAD_OBJ_INIT();
  108.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  109.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  110.   /* If we ever changed/used the texture or orientation state
  111.      of quadObj, we'd need to change it to the defaults here
  112.      with gluQuadricTexture and/or gluQuadricOrientation. */
  113.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  114. }
  115.  
  116. void glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks)
  117. {
  118.   QUAD_OBJ_INIT();
  119.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  120.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  121.   /* If we ever changed/used the texture or orientation state
  122.      of quadObj, we'd need to change it to the defaults here
  123.      with gluQuadricTexture and/or gluQuadricOrientation. */
  124.   gluCylinder(quadObj, base, 0.0, height, slices, stacks);
  125. }
  126.  
  127. /* ENDCENTRY */
  128.  
  129. static void
  130. drawBox(GLfloat size, GLenum type)
  131. {
  132.   static GLfloat n[6][3] =
  133.   {
  134.     {-1.0, 0.0, 0.0},
  135.     {0.0, 1.0, 0.0},
  136.     {1.0, 0.0, 0.0},
  137.     {0.0, -1.0, 0.0},
  138.     {0.0, 0.0, 1.0},
  139.     {0.0, 0.0, -1.0}
  140.   };
  141.   static GLint faces[6][4] =
  142.   {
  143.     {0, 1, 2, 3},
  144.     {3, 2, 6, 7},
  145.     {7, 6, 5, 4},
  146.     {4, 5, 1, 0},
  147.     {5, 6, 2, 1},
  148.     {7, 4, 0, 3}
  149.   };
  150.   GLfloat v[8][3];
  151.   GLint i;
  152.  
  153.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
  154.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
  155.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
  156.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
  157.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
  158.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
  159.  
  160.   for (i = 5; i >= 0; i--) {
  161.     glBegin(type);
  162.     glNormal3fv(&n[i][0]);
  163.     glVertex3fv(&v[faces[i][0]][0]);
  164.     glVertex3fv(&v[faces[i][1]][0]);
  165.     glVertex3fv(&v[faces[i][2]][0]);
  166.     glVertex3fv(&v[faces[i][3]][0]);
  167.     glEnd();
  168.   }
  169. }
  170.  
  171. /* CENTRY */
  172. void glutWireCube(GLdouble size)
  173. {
  174.   drawBox(size, GL_LINE_LOOP);
  175. }
  176.  
  177. void glutSolidCube(GLdouble size)
  178. {
  179.   drawBox(size, GL_QUADS);
  180. }
  181.  
  182. /* ENDCENTRY */
  183.  
  184. static void
  185. doughnut(GLfloat r, GLfloat R, GLint nsides, GLint rings)
  186. {
  187.   int i, j;
  188.   GLfloat theta, phi, theta1;
  189.   GLfloat cosTheta, sinTheta;
  190.   GLfloat cosTheta1, sinTheta1;
  191.   GLfloat ringDelta, sideDelta;
  192.  
  193.   ringDelta = 2.0 * M_PI / rings;
  194.   sideDelta = 2.0 * M_PI / nsides;
  195.  
  196.   theta = 0.0;
  197.   cosTheta = 1.0;
  198.   sinTheta = 0.0;
  199.   for (i = rings - 1; i >= 0; i--) {
  200.     theta1 = theta + ringDelta;
  201.     cosTheta1 = cos(theta1);
  202.     sinTheta1 = sin(theta1);
  203.     glBegin(GL_QUAD_STRIP);
  204.     phi = 0.0;
  205.     for (j = nsides; j >= 0; j--) {
  206.       GLfloat cosPhi, sinPhi, dist;
  207.  
  208.       phi += sideDelta;
  209.       cosPhi = cos(phi);
  210.       sinPhi = sin(phi);
  211.       dist = R + r * cosPhi;
  212.  
  213.       glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
  214.       glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
  215.       glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
  216.       glVertex3f(cosTheta * dist, -sinTheta * dist,  r * sinPhi);
  217.     }
  218.     glEnd();
  219.     theta = theta1;
  220.     cosTheta = cosTheta1;
  221.     sinTheta = sinTheta1;
  222.   }
  223. }
  224.  
  225. /* CENTRY */
  226. void glutWireTorus(GLdouble innerRadius, GLdouble outerRadius,
  227.   GLint nsides, GLint rings)
  228. {
  229.   glPushAttrib(GL_POLYGON_BIT);
  230.   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  231.   doughnut(innerRadius, outerRadius, nsides, rings);
  232.   glPopAttrib();
  233. }
  234.  
  235. void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius,
  236.   GLint nsides, GLint rings)
  237. {
  238.   doughnut(innerRadius, outerRadius, nsides, rings);
  239. }
  240.  
  241. /* ENDCENTRY */
  242.  
  243. static GLfloat dodec[20][3];
  244.  
  245. static void
  246. initDodecahedron(void)
  247. {
  248.   GLfloat alpha, beta;
  249.  
  250.   alpha = sqrt(2.0 / (3.0 + sqrt(5.0)));
  251.   beta = 1.0 + sqrt(6.0 / (3.0 + sqrt(5.0)) -
  252.     2.0 + 2.0 * sqrt(2.0 / (3.0 + sqrt(5.0))));
  253.   /* *INDENT-OFF* */
  254.   dodec[0][0] = -alpha; dodec[0][1] = 0; dodec[0][2] = beta;
  255.   dodec[1][0] = alpha; dodec[1][1] = 0; dodec[1][2] = beta;
  256.   dodec[2][0] = -1; dodec[2][1] = -1; dodec[2][2] = -1;
  257.   dodec[3][0] = -1; dodec[3][1] = -1; dodec[3][2] = 1;
  258.   dodec[4][0] = -1; dodec[4][1] = 1; dodec[4][2] = -1;
  259.   dodec[5][0] = -1; dodec[5][1] = 1; dodec[5][2] = 1;
  260.   dodec[6][0] = 1; dodec[6][1] = -1; dodec[6][2] = -1;
  261.   dodec[7][0] = 1; dodec[7][1] = -1; dodec[7][2] = 1;
  262.   dodec[8][0] = 1; dodec[8][1] = 1; dodec[8][2] = -1;
  263.   dodec[9][0] = 1; dodec[9][1] = 1; dodec[9][2] = 1;
  264.   dodec[10][0] = beta; dodec[10][1] = alpha; dodec[10][2] = 0;
  265.   dodec[11][0] = beta; dodec[11][1] = -alpha; dodec[11][2] = 0;
  266.   dodec[12][0] = -beta; dodec[12][1] = alpha; dodec[12][2] = 0;
  267.   dodec[13][0] = -beta; dodec[13][1] = -alpha; dodec[13][2] = 0;
  268.   dodec[14][0] = -alpha; dodec[14][1] = 0; dodec[14][2] = -beta;
  269.   dodec[15][0] = alpha; dodec[15][1] = 0; dodec[15][2] = -beta;
  270.   dodec[16][0] = 0; dodec[16][1] = beta; dodec[16][2] = alpha;
  271.   dodec[17][0] = 0; dodec[17][1] = beta; dodec[17][2] = -alpha;
  272.   dodec[18][0] = 0; dodec[18][1] = -beta; dodec[18][2] = alpha;
  273.   dodec[19][0] = 0; dodec[19][1] = -beta; dodec[19][2] = -alpha;
  274.   /* *INDENT-ON* */
  275.  
  276. }
  277.  
  278. #define DIFF3(_a,_b,_c) { \
  279.     (_c)[0] = (_a)[0] - (_b)[0]; \
  280.     (_c)[1] = (_a)[1] - (_b)[1]; \
  281.     (_c)[2] = (_a)[2] - (_b)[2]; \
  282. }
  283.  
  284. static void
  285. crossprod(GLfloat v1[3], GLfloat v2[3], GLfloat prod[3])
  286. {
  287.   GLfloat p[3];         /* in case prod == v1 or v2 */
  288.  
  289.   p[0] = v1[1] * v2[2] - v2[1] * v1[2];
  290.   p[1] = v1[2] * v2[0] - v2[2] * v1[0];
  291.   p[2] = v1[0] * v2[1] - v2[0] * v1[1];
  292.   prod[0] = p[0];
  293.   prod[1] = p[1];
  294.   prod[2] = p[2];
  295. }
  296.  
  297. static void
  298. normalize(GLfloat v[3])
  299. {
  300.   GLfloat d;
  301.  
  302.   d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  303.   if (d == 0.0) {
  304. //    __glutWarning("normalize: zero length vector");
  305.     v[0] = d = 1.0;
  306.   }
  307.   d = 1 / d;
  308.   v[0] *= d;
  309.   v[1] *= d;
  310.   v[2] *= d;
  311. }
  312.  
  313. static void
  314. pentagon(int a, int b, int c, int d, int e, GLenum shadeType)
  315. {
  316.   GLfloat n0[3], d1[3], d2[3];
  317.  
  318.   DIFF3(dodec[a], dodec[b], d1);
  319.   DIFF3(dodec[b], dodec[c], d2);
  320.   crossprod(d1, d2, n0);
  321.   normalize(n0);
  322.  
  323.   glBegin(shadeType);
  324.   glNormal3fv(n0);
  325.   glVertex3fv(&dodec[a][0]);
  326.   glVertex3fv(&dodec[b][0]);
  327.   glVertex3fv(&dodec[c][0]);
  328.   glVertex3fv(&dodec[d][0]);
  329.   glVertex3fv(&dodec[e][0]);
  330.   glEnd();
  331. }
  332.  
  333. static void
  334. dodecahedron(GLenum type)
  335. {
  336.   static int inited = 0;
  337.  
  338.   if (inited == 0) {
  339.     inited = 1;
  340.     initDodecahedron();
  341.   }
  342.   pentagon(0, 1, 9, 16, 5, type);
  343.   pentagon(1, 0, 3, 18, 7, type);
  344.   pentagon(1, 7, 11, 10, 9, type);
  345.   pentagon(11, 7, 18, 19, 6, type);
  346.   pentagon(8, 17, 16, 9, 10, type);
  347.   pentagon(2, 14, 15, 6, 19, type);
  348.   pentagon(2, 13, 12, 4, 14, type);
  349.   pentagon(2, 19, 18, 3, 13, type);
  350.   pentagon(3, 0, 5, 12, 13, type);
  351.   pentagon(6, 15, 8, 10, 11, type);
  352.   pentagon(4, 17, 8, 15, 14, type);
  353.   pentagon(4, 12, 5, 16, 17, type);
  354. }
  355.  
  356. /* CENTRY */
  357. void glutWireDodecahedron(void)
  358. {
  359.   dodecahedron(GL_LINE_LOOP);
  360. }
  361.  
  362. void glutSolidDodecahedron(void)
  363. {
  364.   dodecahedron(GL_TRIANGLE_FAN);
  365. }
  366.  
  367. /* ENDCENTRY */
  368.  
  369. static void
  370. recorditem(GLfloat * n1, GLfloat * n2, GLfloat * n3,
  371.   GLenum shadeType)
  372. {
  373.   GLfloat q0[3], q1[3];
  374.  
  375.   DIFF3(n1, n2, q0);
  376.   DIFF3(n2, n3, q1);
  377.   crossprod(q0, q1, q1);
  378.   normalize(q1);
  379.  
  380.   glBegin(shadeType);
  381.   glNormal3fv(q1);
  382.   glVertex3fv(n1);
  383.   glVertex3fv(n2);
  384.   glVertex3fv(n3);
  385.   glEnd();
  386. }
  387.  
  388. static void
  389. subdivide(GLfloat * v0, GLfloat * v1, GLfloat * v2,
  390.   GLenum shadeType)
  391. {
  392.   int depth;
  393.   GLfloat w0[3], w1[3], w2[3];
  394.   GLfloat l;
  395.   int i, j, k, n;
  396.  
  397.   depth = 1;
  398.   for (i = 0; i < depth; i++) {
  399.     for (j = 0; i + j < depth; j++) {
  400.       k = depth - i - j;
  401.       for (n = 0; n < 3; n++) {
  402.         w0[n] = (i * v0[n] + j * v1[n] + k * v2[n]) / depth;
  403.         w1[n] = ((i + 1) * v0[n] + j * v1[n] + (k - 1) * v2[n])
  404.           / depth;
  405.         w2[n] = (i * v0[n] + (j + 1) * v1[n] + (k - 1) * v2[n])
  406.           / depth;
  407.       }
  408.       l = sqrt(w0[0] * w0[0] + w0[1] * w0[1] + w0[2] * w0[2]);
  409.       w0[0] /= l;
  410.       w0[1] /= l;
  411.       w0[2] /= l;
  412.       l = sqrt(w1[0] * w1[0] + w1[1] * w1[1] + w1[2] * w1[2]);
  413.       w1[0] /= l;
  414.       w1[1] /= l;
  415.       w1[2] /= l;
  416.       l = sqrt(w2[0] * w2[0] + w2[1] * w2[1] + w2[2] * w2[2]);
  417.       w2[0] /= l;
  418.       w2[1] /= l;
  419.       w2[2] /= l;
  420.       recorditem(w1, w0, w2, shadeType);
  421.     }
  422.   }
  423. }
  424.  
  425. static void
  426. drawtriangle(int i, GLfloat data[][3], int ndx[][3],
  427.   GLenum shadeType)
  428. {
  429.   GLfloat *x0, *x1, *x2;
  430.  
  431.   x0 = data[ndx[i][0]];
  432.   x1 = data[ndx[i][1]];
  433.   x2 = data[ndx[i][2]];
  434.   subdivide(x0, x1, x2, shadeType);
  435. }
  436.  
  437. /* octahedron data: The octahedron produced is centered at the
  438.    origin and has radius 1.0 */
  439. static GLfloat odata[6][3] =
  440. {
  441.   {1.0, 0.0, 0.0},
  442.   {-1.0, 0.0, 0.0},
  443.   {0.0, 1.0, 0.0},
  444.   {0.0, -1.0, 0.0},
  445.   {0.0, 0.0, 1.0},
  446.   {0.0, 0.0, -1.0}
  447. };
  448.  
  449. static int ondex[8][3] =
  450. {
  451.   {0, 4, 2},
  452.   {1, 2, 4},
  453.   {0, 3, 4},
  454.   {1, 4, 3},
  455.   {0, 2, 5},
  456.   {1, 5, 2},
  457.   {0, 5, 3},
  458.   {1, 3, 5}
  459. };
  460.  
  461. static void
  462. octahedron(GLenum shadeType)
  463. {
  464.   int i;
  465.  
  466.   for (i = 7; i >= 0; i--) {
  467.     drawtriangle(i, odata, ondex, shadeType);
  468.   }
  469. }
  470.  
  471. /* CENTRY */
  472. void glutWireOctahedron(void)
  473. {
  474.   octahedron(GL_LINE_LOOP);
  475. }
  476.  
  477. void glutSolidOctahedron(void)
  478. {
  479.   octahedron(GL_TRIANGLES);
  480. }
  481.  
  482. /* ENDCENTRY */
  483.  
  484. /* icosahedron data: These numbers are rigged to make an
  485.    icosahedron of radius 1.0 */
  486.  
  487. #define X .525731112119133606
  488. #define Z .850650808352039932
  489.  
  490. static GLfloat idata[12][3] =
  491. {
  492.   {-X, 0, Z},
  493.   {X, 0, Z},
  494.   {-X, 0, -Z},
  495.   {X, 0, -Z},
  496.   {0, Z, X},
  497.   {0, Z, -X},
  498.   {0, -Z, X},
  499.   {0, -Z, -X},
  500.   {Z, X, 0},
  501.   {-Z, X, 0},
  502.   {Z, -X, 0},
  503.   {-Z, -X, 0}
  504. };
  505.  
  506. static int index[20][3] =
  507. {
  508.   {0, 4, 1},
  509.   {0, 9, 4},
  510.   {9, 5, 4},
  511.   {4, 5, 8},
  512.   {4, 8, 1},
  513.   {8, 10, 1},
  514.   {8, 3, 10},
  515.   {5, 3, 8},
  516.   {5, 2, 3},
  517.   {2, 7, 3},
  518.   {7, 10, 3},
  519.   {7, 6, 10},
  520.   {7, 11, 6},
  521.   {11, 0, 6},
  522.   {0, 1, 6},
  523.   {6, 1, 10},
  524.   {9, 0, 11},
  525.   {9, 11, 2},
  526.   {9, 2, 5},
  527.   {7, 2, 11},
  528. };
  529.  
  530. static void
  531. icosahedron(GLenum shadeType)
  532. {
  533.   int i;
  534.  
  535.   for (i = 19; i >= 0; i--) {
  536.     drawtriangle(i, idata, index, shadeType);
  537.   }
  538. }
  539.  
  540. /* CENTRY */
  541. void glutWireIcosahedron(void)
  542. {
  543.   icosahedron(GL_LINE_LOOP);
  544. }
  545.  
  546. void glutSolidIcosahedron(void)
  547. {
  548.   icosahedron(GL_TRIANGLES);
  549. }
  550.  
  551. /* ENDCENTRY */
  552.  
  553. /* tetrahedron data: */
  554.  
  555. #define T       1.73205080756887729
  556.  
  557. static GLfloat tdata[4][3] =
  558. {
  559.   {T, T, T},
  560.   {T, -T, -T},
  561.   {-T, T, -T},
  562.   {-T, -T, T}
  563. };
  564.  
  565. static int tndex[4][3] =
  566. {
  567.   {0, 1, 3},
  568.   {2, 1, 0},
  569.   {3, 2, 0},
  570.   {1, 2, 3}
  571. };
  572.  
  573. static void
  574. tetrahedron(GLenum shadeType)
  575. {
  576.   int i;
  577.  
  578.   for (i = 3; i >= 0; i--)
  579.     drawtriangle(i, tdata, tndex, shadeType);
  580. }
  581.  
  582. /* CENTRY */
  583. void glutWireTetrahedron(void)
  584. {
  585.   tetrahedron(GL_LINE_LOOP);
  586. }
  587.  
  588. void glutSolidTetrahedron(void)
  589. {
  590.   tetrahedron(GL_TRIANGLES);
  591. }
  592.  
  593. /* ENDCENTRY */
  594.